//
//  KYCatogryView.m
//  quickDoctor
//
//  Created by sdos on 2017/3/11.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "KYCatogryView.h"

#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
// 宏定义 随机颜色
#define RandomColor RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

@interface KYCatogryView ()

@property (nonatomic, strong) NSMutableArray <UIButton *> *buttonArray;

//上一个被选中的按钮
@property (nonatomic, weak) UIButton *lastButton;
//第一个按钮
@property (nonatomic, weak) UIButton *firstButton;

@property (nonatomic, weak) UIView *singleLine;

@end

@implementation KYCatogryView

- (NSMutableArray <UIButton *> *)buttonArray {
    if (nil == _buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    
    return _buttonArray;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //设置button
    [self setupButton];
    // 默认选中第一个按钮
    [self clickButton:_firstButton];
    
    //设置细线
    [self setupLine];
}

#pragma mark
#pragma mark -设置细线
- (void)setupLine {
    
    UIView *singleLine = [[UIView alloc] init];
    
    _singleLine = singleLine;
    
    singleLine.backgroundColor = RGBColor(47, 198, 197);
    
    [self addSubview:singleLine];
    
    // 约束
    [singleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.equalTo(_firstButton);
        make.top.equalTo(_firstButton.mas_bottom).offset(4);
        make.height.mas_equalTo(2);
    }];
}
#pragma mark
#pragma mark - 设置button
- (void)setupButton
{
    NSArray *titleArray = @[@"就诊条件", @"医生简介"];
    
    int index = 0;
    
    for (int i=0 ;i<2;i++) {
        
        UIButton *button = [self buttonWithTitle:titleArray[i]];
        //设置tag值
        button.tag = index++;
        // 添加点击事件
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

        [self.buttonArray addObject:button];
        
        [self addSubview:button];
    }
    // 为第一个按钮赋值
    _firstButton = self.buttonArray[0];
    
    //添加左侧button约束
    [self.buttonArray[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(50);
        make.centerY.equalTo(self);
    }];
    
    
    //添加中间分割线
    UIImageView *lineView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(1, 40));
    }];
    //添加右侧button约束
    [self.buttonArray[1] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.offset(-50);
        make.centerY.equalTo(self);
    }];
    
    // 立即刷新
    [self layoutIfNeeded];

    
}

#pragma mark
#pragma mark - 按钮点击
- (void)clickButton:(UIButton *)button {
    // 先把上一个选中的按钮设置为NO
    _lastButton.selected = NO;
    
    // 修改当前点击的按钮
    button.selected = YES;
    
    // 赋值给lastButton
    _lastButton = button;
    
    CGFloat w = self.buttonArray[1].center.x - self.buttonArray[0].center.x;
    
    // 更新细线的约束, centerX
    [_singleLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_firstButton).offset(w * button.tag);
    }];
    //实现动画效果
    [UIView animateWithDuration:0.1 animations:^{
        [self layoutIfNeeded];
    }];
    
    
    //
    if ([self.delegate respondsToSelector:@selector(CatogryView:Selection:)]) {
        
        [self.delegate CatogryView:self Selection:button.tag];
    }
}

- (void)setRatio:(CGFloat)ratio
{
    _ratio = ratio;
    [_singleLine mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_firstButton).offset(self.width *ratio);
    }];
    [self layoutIfNeeded];
}

- (void)setItemTag:(NSInteger)itemTag
{
    _itemTag = itemTag;
    if (_itemTag == 0) {
        _buttonArray[0].selected = YES;
        _buttonArray[1].selected = NO;
    } else{
        _buttonArray[0].selected = NO;
        _buttonArray[1].selected = YES;
    }
}

#pragma mark
#pragma mark - 实例化按钮
- (UIButton *)buttonWithTitle:(NSString *)title {
    
    UIButton *button = [[UIButton alloc] init];
    
    // 设置文字
    [button setTitle:title forState:UIControlStateNormal];
    
    // 设置文字颜色
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [button setTitleColor:RGBColor(47, 198, 197) forState:UIControlStateSelected];
    
    // 文字大小
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    
    return button;
}

@end































